﻿Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

Namespace Load

    ''' <summary>
    ''' クローンエクスポーター
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <remarks></remarks>
    Public Class CloneableExporter(Of T)
        Implements ICloneableExporter(Of T)

        Public Sub New(lst As List(Of T))
            Me.InnerList = lst
        End Sub

        Private Property InnerList As IList(Of T)

        Public Function Clone() As ICloneableExporter(Of T) Implements ICloneableExporter(Of T).Clone
            Return CloneableExporter.Clone(Me.InnerList)
        End Function

        Public Function ToArray() As T() Implements ICloneableExporter(Of T).ToArray
            Return Me.InnerList.ToArray
        End Function

        Public Function ToItem() As T Implements ICloneableExporter(Of T).ToItem
            If Me.InnerList.Count <> 1 Then Return Nothing
            Return Me.InnerList.First
        End Function

        Public Function ToList() As System.Collections.Generic.List(Of T) Implements ICloneableExporter(Of T).ToList
            Return Me.InnerList
        End Function

    End Class

    ''' <summary>
    ''' クローンエクスポーター
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CloneableExporter
        Public Shared Function Clone(Of T)(lst As IList(Of T)) As ICloneableExporter(Of T)
            Using ms As New MemoryStream
                Dim bf As New BinaryFormatter
                bf.Serialize(ms, lst)
                ms.Position = 0
                Return New CloneableExporter(Of T)(bf.Deserialize(ms))
            End Using
        End Function
    End Class

End Namespace
